#include "ParserStatus.h"
#include "Token.h"

using namespace std;

CParserStatus::CParserStatus()
{

}

CParserStatus::~CParserStatus()
{

}

int CParserStatus::AddAction( Token_Type Token, _Action Action )
{
    if (m_Action.find(Token) == m_Action.end())
    {
        m_Action[Token] = Action;
        return 1;
    }
    else
    {
        _Action ExistAction = m_Action[Token];
        if (ExistAction.Action == Action.Action 
            && ExistAction.ProductionIndex == Action.ProductionIndex
            && ExistAction.StatusIndex == Action.StatusIndex)
        {
            return 1;
        }
    }

    return 0;
}

int CParserStatus::AddGoto( Token_Type Token, _GOTO Goto )
{
    if (m_Goto.find(Token) == m_Goto.end())
    {
        m_Goto[Token] = Goto;
        return 1;
    }
    else
    {
        _GOTO ExistGoto = m_Goto[Token];
        if (ExistGoto.StatusIndex == Goto.StatusIndex)
        {
            return 1;
        }
    }

    return 0;
}


void CParserStatus::Log()
{
	for (map<Token_Type, _Action>::iterator it = m_Action.begin(); it != m_Action.end(); ++it)
	{
		LOG("On %s , Action Type: %d, ProductionIndex: %d, StatusIndex: %d",TOKEN_DEBUG_STRING(it->first).c_str(),it->second.Action,it->second.ProductionIndex,it->second.StatusIndex);
	}

	for (map<Token_Type, _GOTO>::iterator it = m_Goto.begin(); it != m_Goto.end(); ++it)
	{
		LOG("On %s , Goto StatusIndex: %d",TOKEN_DEBUG_STRING(it->first).c_str(),it->second.StatusIndex);
	}
}

_Action CParserStatus::GetAction( Token_Type Token )
{

	map<Token_Type, _Action>::iterator it;
	if ((it = m_Action.find(Token)) != m_Action.end())
	{
		return it->second;
	}
	else
	{
		_Action Action;
		Action.Action = ACTION_ERROR;
		return Action;
	}
}

_GOTO CParserStatus::GetGoto( Token_Type Token )
{
	map<Token_Type, _GOTO>::iterator it;
	if ((it = m_Goto.find(Token)) != m_Goto.end())
	{
		return it->second;
	}
	else
	{
		_GOTO Goto;
		Goto.StatusIndex = -1;
		return Goto;
	}
}